package com.slwish.xty.ui.activity;

import android.content.Intent;
import android.view.View;

import com.slwish.common.base.PresenterToolbarActivity;
import com.slwish.common.config.Contants;
import com.slwish.common.interf.PerfectClickListener;
import com.slwish.common.utils.SpUtils;
import com.slwish.xty.R;
import com.slwish.xty.entity.UserModel;
import com.slwish.xty.mvp.contract.ModifyNameContract;
import com.slwish.xty.mvp.presenter.ModifyNamePresenter;
import com.slwish.xty.widget.ClearEditText;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

/**
 * 修改昵称
 */
public class ModifyNameActivity extends PresenterToolbarActivity<ModifyNameContract.Presenter> implements ModifyNameContract.View {

    @BindView(R.id.et_nick_name)
    ClearEditText etNickName;
    private String token;

    @Override
    protected int getLayoutId() {
        return R.layout.act_modify_name;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        setTitle(R.string.user_info_name_text);
        setRightText(R.string.save_text);
        token = (String) SpUtils.get(mContext, Contants.UserToken, "");
        setRightOnClickListener(new PerfectClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mPresenter.modify(mContext, etNickName.getText().toString(), token);
//                long userid = SpUtils.getLong(mContext,Contants.UserCode,0);
//                UserModel query = UserDb.query(userid + "");
//                query.setNickname(etNickName.getText().toString());
//                query.saveOrUpdate("userid = ?",query.getUserId()+"");
//                modifySuccess(query);
            }
        });
    }

    /**
     * 修改成功
     * @param model
     */
    @Override
    public void modifySuccess(UserModel model) {
//        EventBus.getDefault().post(new UserEvent("",model.getUserId()));
        EventBus.getDefault().post(model);
        Intent intent = new Intent();
        intent.putExtra("Nickname", model.getNickname());
        setResult(Contants.RESULT_CODE, intent);
        finish();
    }

    @Override
    protected ModifyNameContract.Presenter initPresenter() {
        return new ModifyNamePresenter(this);
    }
}
